package org.testfire.realm.model.entities.testfire;

import javax.persistence.*;
import static javax.persistence.GenerationType.*;

import org.testfire.realm.util.S;

/**
 * Created by IntelliJ IDEA.
 * User: granum
 * Date: Aug 26, 2007
 * Time: 2:16:10 PM
 */
@Entity
@Table(catalog = "testfire", name = "crontask")
public class CrontaskEntity
{

    private Long id;

    private String name;

    private String description;

    private String min;

    private String hour;

    private String dom;

    private String month;

    private String dow;

    private boolean manual;

    /**
     * Default constructor creates a task which repeats every weekday at 8pm (20:00)
     */
    public CrontaskEntity()
    {
        this.min = "*";
        this.hour = "20";
        this.dom = "*";
        this.month = "*";
        this.dow = "1,2,3,4,5";
    }

    /**
     * Populate this cron-like task via a (pretty much) standard cron string.
     * Format: "minute hour dom month dow"
     * Unlike cron, no user or cmd is needed/used.
     * <p/>
     * <p/>
     * minute	This controls what minute of the hour the command will run on,
     * and is between '0' and '59'
     * hour	This controls what hour the command will run on, and is specified in
     * the 24 hour clock, values must be between 0 and 23 (0 is midnight)
     * dom	This is the Day of Month, that you want the command run on, e.g. to
     * run a command on the 19th of each month, the dom would be 19.
     * month	This is the month a specified command will run on, it may be specified
     * numerically (0-12), or as the name of the month (e.g. May)
     * dow	This is the Day of Week that you want a command to be run on, it can
     * also be numeric (0-7) or as the name of the day (e.g. sun).
     * <p/>
     * Comma seperated lists of values are allowed for each field.
     * <p/>
     * The string is space-delimited, therefore each individual field may NOT contain spaces!
     * Examples:
     * "This command is run at one min past every hour"
     * "01 * * * *"
     * "This command is run daily at 8:17 am"
     * "17 8 * * *"
     * "This command is run daily at 8:17 pm"
     * "17 20 * * *"
     * "This command is run at 4 am every Sunday"
     * "00 4 * * 0"
     * "So is this"
     * "* 4 * * Sun"
     * "This command is run 4:42 am every 1st of the month"
     * "42 4 1 * *"
     * "This command is run hourly on the 19th of July"
     * "01 * 19 07 *"
     * Run every midday between the 1st and the 15th as well as the 20th
     * and 25th (inclusive) and also on the 17th of every month.
     * "* 12 1-15,17,20-25 * *" (@todo implement range!)
     * <p/>
     * Instructions borrowed from: http://www.unixgeeks.org/security/newbie/unix/cron-1.html
     *
     * @param cronString A string of the format "minute hour dom month dow".
     */
    @Transient
    public void setCronString( String cronString )
    {
        if ( S.empty( cronString ) || cronString.equals( "manual" ) )
        {
            this.manual = true;
        }
        else
        {
            String[] vals = cronString.split( " " );
            this.min = vals[0];
            this.hour = vals[1];
            this.dom = vals[2];
            this.month = vals[3];
            this.dow = vals[4];
        }
    }

    @Transient
    public String getCronString()
    {
        return manual ? "manual" : this.min + " " + this.hour + " " + this.dom + " " + this.month + " " + this.dow;
    }

    @Basic
    @Column(name = "description", nullable = false, length = 65535)
    public String getDescription()
    {
        return description;
    }

    public void setDescription( String description )
    {
        this.description = description;
    }

    @Basic
    @Column(name = "name", nullable = false)
    public String getName()
    {
        return name;
    }

    public void setName( String name )
    {
        this.name = name;
    }

    @Basic
    @Column(name = "dow", nullable = false, length = 45)
    public String getDow()
    {
        return dow;
    }

    public void setDow( String dow )
    {
        this.dow = dow;
    }

    @Basic
    @Column(name = "is_manual", nullable = false)
    public boolean isManual()
    {
        return manual;
    }

    public void setManual( boolean isManual )
    {
        this.manual = isManual;
    }

    @Basic
    @Column(name = "month", nullable = false, length = 45)
    public String getMonth()
    {
        return month;
    }

    public void setMonth( String month )
    {
        this.month = month;
    }

    @Basic
    @Column(name = "dom", nullable = false, length = 45)
    public String getDom()
    {
        return dom;
    }

    public void setDom( String dom )
    {
        this.dom = dom;
    }

    @Basic
    @Column(name = "hour", nullable = false, length = 45)
    public String getHour()
    {
        return hour;
    }

    public void setHour( String hour )
    {
        this.hour = hour;
    }

    @Basic
    @Column(name = "min", nullable = false, length = 45)
    public String getMin()
    {
        return min;
    }

    public void setMin( String min )
    {
        this.min = min;
    }

    @Id @GeneratedValue(strategy=SEQUENCE)
    public Long getId()
    {
        return id;
    }

    public void setId( Long id )
    {
        this.id = id;
    }
}
